package com.world.business.realm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.realm.domain.Realm;
import com.world.business.realm.domain.query.RealmAddRequest;
import com.world.business.realm.domain.query.RealmPageRequest;
import com.world.business.realm.domain.query.RealmUpdateRequest;
import com.world.business.realm.domain.vo.RealmVO;
import com.world.business.realm.service.IRealmService;
import com.world.business.role.domain.Role;
import com.world.business.role.service.IRoleService;
import com.world.common.controller.BaseController;
import com.world.common.domain.PageResult;
import com.world.common.domain.ResponseData;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 境界表 前端控制器
 * </p>
 *
 * @author 邓驴
 * @since 2023-05-16
 */
@Api(tags = "境界")
@RestController
@RequestMapping("/realm")
public class RealmController extends BaseController<Realm> {

    @Resource
    private IRealmService realmService;
    @Resource
    private IRoleService roleService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<PageResult<RealmVO>> list(RealmPageRequest request) {

        Page<Realm> page = startPage(request);

        LambdaQueryWrapper<Realm> queryWrapper = Wrappers.lambdaQuery(Realm.class);
        queryWrapper.eq(Realm::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.orderByAsc(Realm::getRank);
        Page<Realm> result = realmService.page(page, queryWrapper);

        LambdaQueryWrapper<Role> lambdaQuery = Wrappers.lambdaQuery(Role.class);
        lambdaQuery.eq(Role::getDelFlag, DelFlagEnum.NORMAL.getCode());
        List<Role> list = roleService.list(lambdaQuery);
        Map<Long, String> roleMap = list.stream().collect(Collectors.toMap(Role::getId, Role::getRoleName));

        PageResult<RealmVO> pageResult = getPageResult(result, RealmVO.class);
        List<RealmVO> records = pageResult.getRecords();
        records.forEach(i -> i.setRoleName(roleMap.get(Long.valueOf(i.getRoleId()))));
        return new SuccessResponseData<>(pageResult);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseData<Long> add(@RequestBody @Validated RealmAddRequest request) {
        return new SuccessResponseData<>(realmService.add(request));
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseData<RealmVO> get(@PathVariable("id") Long id) {
        return new SuccessResponseData<>(realmService.get(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ResponseData<Void> delete(@PathVariable("id") Long id) {
        realmService.delete(id);
        return new SuccessResponseData<>();
    }

    @ApiOperation("更新")
    @PutMapping("/update")
    public ResponseData<Void> update(@RequestBody @Validated RealmUpdateRequest request) {
        realmService.update(request);
        return new SuccessResponseData<>();
    }

}
